using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace RF_Jammer_Base
{
    class Particle
    {
        private int id;
        private ArrayList TargetNodes;
        private ArrayList SourceNodes;

        public Particle(int id, ArrayList TargetNodes, ArrayList SourceNodes)
        {
            this.id = id;
            this.TargetNodes = TargetNodes;
            this.SourceNodes = SourceNodes;
            this.InitParticle();
        }

        public void InitParticle()
        {
            foreach (SourceNode sNode in SourceNodes)
            {
                sNode.InitRandom();
            }
        }

        /* function to caluculate and update particles fitness value */
        public double UpdateFitness()
        {
            double fitness = 0;

            foreach (TargetNode tNode in this.TargetNodes)
            {
                tNode.CalcIncidentPower(this.SourceNodes);
            }

            double totalOuptutPower = 0;
            double totalIncidentPower = 0;

            foreach (SourceNode sNode in this.SourceNodes)
            {
                totalOuptutPower += sNode.OutputPower;
            }

            foreach (TargetNode tNode in this.TargetNodes)
            {
                totalIncidentPower += tNode.IncidentPower;
                /* check if target node has been jammed */
                if (tNode.IncidentPower >= tNode.JammingPower)
                {
                    fitness += 2.0;
                }
                else
                {
                    fitness += tNode.IncidentPower / tNode.JammingPower;
                }
            }

            fitness += (totalIncidentPower / totalOuptutPower);

            return fitness;
        }

        /* customized to string method for the particle object */
        public override string ToString()
        {
            string str = "";
            str += string.Format("Particle {0}\n", id);

            str += "--- TARGET NODES ---\n";
            foreach (TargetNode tNode in TargetNodes)
            {
                str += tNode.ToString();
            }

            str += "--- SOURCE NODES ---\n";
            foreach (SourceNode sNode in SourceNodes)
            {
                str += sNode.ToString();
            }

            return str;
        }

        /* public getter and setter for id property of particle */
        public int ID
        {
            get
            {
                return id;
            }
            set
            {
                id = value;
            }
        }

    }
}
